//
//  UIImage+Category.m
//  999-UIImage 旋转
//
//  Created by 李晓东 on 17/11/11.
//  Copyright © 2017年 PeanutXu. All rights reserved.
//

#import "UIImage+Category.h"

@implementation UIImage (Category)
- (UIImage *)imageRotateOfRadians:(CGFloat)radias{
    CGAffineTransform t = CGAffineTransformMakeRotation(radias);
    CGRect rotateRect = CGRectApplyAffineTransform(CGRectMake(0, 0, self.size.width , self.size.width), t);
    CGSize rotateSize = rotateRect.size;
    
    UIGraphicsBeginImageContext(rotateSize);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(ctx, rotateSize.width / 2, rotateSize.width / 2);
    CGContextRotateCTM(ctx, radias);
    CGContextScaleCTM(ctx, 1.0, 1.0);
    
    CGContextDrawImage(ctx, CGRectMake(-rotateSize.width / 2, - rotateSize.width / 2, rotateSize.width, rotateSize.width), self.CGImage);
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return newImage;
}

- (UIImage *)rescaleImageToSize:(CGSize)size {
    
    CGRect rect = CGRectMake(0.0, 0.0, size.width, size.height);
    
    UIGraphicsBeginImageContext(rect.size);
    
    [self drawInRect:rect];  // scales image to rect
    
    UIImage *resImage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return resImage;
    
}
@end
